package cn.enited.devtools.apiaggregator.controller;

import cn.enited.devtools.apiaggregator.dto.MapActionResult;
import cn.enited.devtools.apiaggregator.service.ApiGoService;
import cn.enited.devtools.apiaggregator.utils.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/go")
public class GoController extends BaseController {

    private ApiGoService goService;

    @Autowired
    public void setGoService(ApiGoService goService) {
        this.goService = goService;
    }

    @RequestMapping(path = "/{projectName}/{apiName}")
    public MapActionResult<String, Object> sendRequest(
            @PathVariable("projectName") String projectName,
            @PathVariable("apiName") String apiName,
            @RequestHeader("env") String environment,
            @RequestBody(required = false) String body) {
        Map<String, Object> result = goService.execute(projectName, environment, apiName, getRequest(), body);
        return ActionUtil.mapResult(result);
    }
}
